<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>logbuffer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_logbuffer.js.html">logbuffer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A buffer for log records. The purpose of this is to improve
<a name="line17"></a> * logging performance by re-using old objects when the buffer becomes full and
<a name="line18"></a> * to eliminate the need for each app to implement their own log buffer. The
<a name="line19"></a> * disadvantage to doing this is that log handlers cannot maintain references to
<a name="line20"></a> * log records and expect that they are not overwriten at a later point.
<a name="line21"></a> *
<a name="line22"></a> * @author agrieve@google.com (Andrew Grieve)
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.debug.LogBuffer&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line28"></a>goog.require(&#39;goog.debug.LogRecord&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Creates the log buffer.
<a name="line34"></a> * @constructor
<a name="line35"></a> */
<a name="line36"></a>goog.debug.LogBuffer = function() {
<a name="line37"></a>  goog.asserts.assert(goog.debug.LogBuffer.isBufferingEnabled(),
<a name="line38"></a>      &#39;Cannot use goog.debug.LogBuffer without defining &#39; +
<a name="line39"></a>      &#39;goog.debug.LogBuffer.CAPACITY.&#39;);
<a name="line40"></a>  this.clear();
<a name="line41"></a>};
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * A static method that always returns the same instance of LogBuffer.
<a name="line46"></a> * @return {!goog.debug.LogBuffer} The LogBuffer singleton instance.
<a name="line47"></a> */
<a name="line48"></a>goog.debug.LogBuffer.getInstance = function() {
<a name="line49"></a>  if (!goog.debug.LogBuffer.instance_) {
<a name="line50"></a>    // This function is written with the return statement after the assignment
<a name="line51"></a>    // to avoid the jscompiler StripCode bug described in http://b/2608064.
<a name="line52"></a>    // After that bug is fixed this can be refactored.
<a name="line53"></a>    goog.debug.LogBuffer.instance_ = new goog.debug.LogBuffer();
<a name="line54"></a>  }
<a name="line55"></a>  return goog.debug.LogBuffer.instance_;
<a name="line56"></a>};
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * @define {number} The number of log records to buffer. 0 means disable
<a name="line61"></a> * buffering.
<a name="line62"></a> */
<a name="line63"></a>goog.debug.LogBuffer.CAPACITY = 0;
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * The array to store the records.
<a name="line68"></a> * @type {!Array.&lt;!goog.debug.LogRecord|undefined&gt;}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.debug.LogBuffer.prototype.buffer_;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * The index of the most recently added record or -1 if there are no records.
<a name="line76"></a> * @type {number}
<a name="line77"></a> * @private
<a name="line78"></a> */
<a name="line79"></a>goog.debug.LogBuffer.prototype.curIndex_;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Whether the buffer is at capacity.
<a name="line84"></a> * @type {boolean}
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>goog.debug.LogBuffer.prototype.isFull_;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Adds a log record to the buffer, possibly overwriting the oldest record.
<a name="line92"></a> * @param {goog.debug.Logger.Level} level One of the level identifiers.
<a name="line93"></a> * @param {string} msg The string message.
<a name="line94"></a> * @param {string} loggerName The name of the source logger.
<a name="line95"></a> * @return {!goog.debug.LogRecord} The log record.
<a name="line96"></a> */
<a name="line97"></a>goog.debug.LogBuffer.prototype.addRecord = function(level, msg, loggerName) {
<a name="line98"></a>  var curIndex = (this.curIndex_ + 1) % goog.debug.LogBuffer.CAPACITY;
<a name="line99"></a>  this.curIndex_ = curIndex;
<a name="line100"></a>  if (this.isFull_) {
<a name="line101"></a>    var ret = this.buffer_[curIndex];
<a name="line102"></a>    ret.reset(level, msg, loggerName);
<a name="line103"></a>    return ret;
<a name="line104"></a>  }
<a name="line105"></a>  this.isFull_ = curIndex == goog.debug.LogBuffer.CAPACITY - 1;
<a name="line106"></a>  return this.buffer_[curIndex] =
<a name="line107"></a>      new goog.debug.LogRecord(level, msg, loggerName);
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * @return {boolean} Whether the log buffer is enabled.
<a name="line113"></a> */
<a name="line114"></a>goog.debug.LogBuffer.isBufferingEnabled = function() {
<a name="line115"></a>  return goog.debug.LogBuffer.CAPACITY &gt; 0;
<a name="line116"></a>};
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Removes all buffered log records.
<a name="line121"></a> */
<a name="line122"></a>goog.debug.LogBuffer.prototype.clear = function() {
<a name="line123"></a>  this.buffer_ = new Array(goog.debug.LogBuffer.CAPACITY);
<a name="line124"></a>  this.curIndex_ = -1;
<a name="line125"></a>  this.isFull_ = false;
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Calls the given function for each buffered log record, starting with the
<a name="line131"></a> * oldest one.
<a name="line132"></a> * @param {function(!goog.debug.LogRecord)} func The function to call.
<a name="line133"></a> */
<a name="line134"></a>goog.debug.LogBuffer.prototype.forEachRecord = function(func) {
<a name="line135"></a>  var buffer = this.buffer_;
<a name="line136"></a>  // Corner case: no records.
<a name="line137"></a>  if (!buffer[0]) {
<a name="line138"></a>    return;
<a name="line139"></a>  }
<a name="line140"></a>  var curIndex = this.curIndex_;
<a name="line141"></a>  var i = this.isFull_ ? curIndex : -1;
<a name="line142"></a>  do {
<a name="line143"></a>    i = (i + 1) % goog.debug.LogBuffer.CAPACITY;
<a name="line144"></a>    func(/** @type {!goog.debug.LogRecord} */ (buffer[i]));
<a name="line145"></a>  } while (i != curIndex);
<a name="line146"></a>};
</pre>


</body>
</html>
